package com.agent.membership.mapper;

import com.agent.membership.entity.CreditTransaction;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 积分交易记录Mapper
 * @author Jeff_Wan
 * @description 积分交易记录数据库操作接口
 */
@Mapper
public interface CreditTransactionMapper extends BaseMapper<CreditTransaction> {

    /**
     * 查询过期积分交易
     * @param now 当前时间(UTC)
     * @return 过期积分列表
     */
    @Select("SELECT * FROM credit_transaction " +
            "WHERE expire_at <= #{now} AND expired = false")
    List<CreditTransaction> findExpiredTransactions(@Param("now") LocalDateTime now);

    /**
     * 查询有效积分交易
     * @param userId 用户ID
     * @param now 当前时间(UTC)
     * @return 有效积分列表
     */
    @Select("SELECT * FROM credit_transaction " +
            "WHERE user_id = #{userId} " +
            "AND expire_at > #{now} " +
            "AND expired = false " +
            "AND amount > 0")
    List<CreditTransaction> findValidTransactions(
            @Param("userId") Long userId,
            @Param("now") LocalDateTime now);
}